package com.diaohaiwen.framework.mq.rocket;

import com.diaohaiwen.framework.mq.rocket.config.ConsumerConfig;
import com.diaohaiwen.framework.mq.rocket.config.GlobalMessageListener;
import com.diaohaiwen.framework.mq.rocket.config.ProducerConfig;
import org.springframework.cloud.commons.util.SpringFactoryImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author 刁海文
 * @since 2024/1/15 23:10
 */
public class EnableRocketImportSelector extends SpringFactoryImportSelector<EnableRocket> {

    @Override
    protected boolean isEnabled() {
        return getEnvironment().getProperty("config.rocket.enable", Boolean.class, Boolean.FALSE);
    }

    @Override
    public String[] selectImports(AnnotationMetadata metadata) {
        String[] strings = super.selectImports(metadata);
        ArrayList<String> list = new ArrayList<>(Arrays.asList(strings));
        if (getEnvironment().getProperty("config.rocket.producer.enable", Boolean.class, Boolean.TRUE)) {
            list.add(ProducerConfig.class.getName());
            list.add(RocketService.class.getName());
        }
        if (getEnvironment().getProperty("config.rocket.consumer.enable", Boolean.class, Boolean.TRUE)) {
            list.add(GlobalMessageListener.class.getName());
            list.add(ConsumerConfig.class.getName());
        }
        return list.toArray(new String[0]);
    }

    @Override
    protected boolean hasDefaultFactory() {
        return true;
    }
}
